package com.hyx.spring.config;

import com.mchange.v2.c3p0.ComboPooledDataSource;
import lombok.Data;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Profile;

import javax.sql.DataSource;
import java.beans.PropertyVetoException;

/**
 * @Author hyx
 * @Description 测试多种数据源
 * 通过@Profile注解加了环境标识的bean，只有这个环境被激活的时候，相应的bean才会被注册到IOC容器中。
 * @Profile("default")——设置为默认的数据源
 * @Date 2024/3/11 11:50
 * @Version 1.0
 */
@Profile("test")
@Configuration
public class ProfileConfig {

    @Profile("dev")
    @Bean("devDatasource")
    public DataSource dataSourceDev() throws PropertyVetoException {
        ComboPooledDataSource dataSource = new ComboPooledDataSource();
        dataSource.setJdbcUrl("jdbc:mysql://localhost:3306/test_dev");
        dataSource.setUser("root");
        dataSource.setPassword("HU2019miqi;");
        dataSource.setDriverClass("com.mysql.jdbc.Driver");
        return dataSource;
    }

    @Profile("test")
    @Bean("testDatasource")
    public DataSource dataSourceTest() throws PropertyVetoException {
        ComboPooledDataSource dataSource = new ComboPooledDataSource();
        dataSource.setJdbcUrl("jdbc:mysql://localhost:3306/test_test");
        dataSource.setUser("root");
        dataSource.setPassword("HU2019miqi;");
        dataSource.setDriverClass("com.mysql.jdbc.Driver");
        return dataSource;
    }

    @Profile("prod")
    @Bean("prodDatasource")
    public DataSource dataSourceProd() throws PropertyVetoException {
        ComboPooledDataSource dataSource = new ComboPooledDataSource();
        dataSource.setJdbcUrl("jdbc:mysql://localhost:3306/test_prod");
        dataSource.setUser("root");
        dataSource.setPassword("HU2019miqi;");
        dataSource.setDriverClass("com.mysql.jdbc.Driver");
        return dataSource;
    }
}
